package com.furongji.education.controller.api;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.furongji.common.result.Result;
import com.furongji.education.domain.dto.ArticleDto;
import com.furongji.education.domain.query.ArticleQuery;
import com.furongji.education.domain.vo.ArticleVo;
import com.furongji.education.service.ArticleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 社区文章表 前端控制器
 * </p>
 *
 * @author furongji
 * @since 2025-06-05
 */
@Tag(name = "用户端文章管理")
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/articles")
@Slf4j
public class ArticleApiController {

    private final ArticleService articleService;

    @Operation(summary = "获取文章列表")
    @PostMapping("/getArticles")
    public Result getArticles(@RequestParam Integer current, @RequestParam Integer size,
                              @RequestBody(required = false) ArticleQuery articleQuery) {
        IPage<ArticleVo> result = articleService.getArticles(current, size, articleQuery);
        return Result.success(result);
    }

    @Operation(summary = "发布文章")
    @PostMapping("/publishArticle")
    public Result publishArticle(@RequestBody ArticleDto articleDto) throws JsonProcessingException {
        System.out.println(articleDto);
        boolean flag = articleService.publishArticle(articleDto);
        return flag ? Result.success() : Result.error();
    }

    @Operation(summary = "文章详情")
    @GetMapping("/articleInfo/{articleId}")
    public Result getArticleInfo(@PathVariable String articleId) {
        ArticleVo articleVo = articleService.getArticleInfo(articleId);
        return Result.success(articleVo);
    }

}
